<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 1. 求数组所有值的和
        let sum = [0, 1, 2, 3].reduce(function (previousValue, currentValue) {
            return previousValue + currentValue
        }, 0);
        console.log(sum);

        // 2. 累加对象数组里的值(计算成绩的总数)
        //     要累加对象数组中包含的值，必须提供 initialValue，以便各个 item 正确通过你的函数

        let initialValue = 0
        let sum2 = [{ x: 1 }, { x: 2 }, { x: 3 }].reduce(
            (previousValue, currentValue) => previousValue + currentValue.x
            , initialValue
        )

        console.log(sum2) // logs 6

        // 3. 将二维数组转化为一维

        let flattened = [[0, 1], [2, 3], [4, 5]].reduce(
            function (previousValue, currentValue) {
                return previousValue.concat(currentValue)
            },
            []
        )
        // flattened is [0, 1, 2, 3, 4, 5]
        console.log(flattened);

        // 4. 计算数组中每个元素出现的次数

        let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']

        let countedNames = names.reduce(function (allNames, name) {
            if (name in allNames) {
                allNames[name]++
            }
            else {
                allNames[name] = 1
            }
            return allNames
        }, {});
        // { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
        console.log(countedNames);

        // 5. 按属性对 object 分类
        let people = [
            { name: 'Alice', age: 21 },
            { name: 'Max', age: 20 },
            { name: 'Jane', age: 20 }
        ];

        function groupBy(objectArray, property) {
            return objectArray.reduce(function (acc, obj) {
                let key = obj[property]
                if (!acc[key]) {
                    acc[key] = [];
                }
                acc[key].push(obj)
                return acc;
            }, {});
        }

        let groupedPeople = groupBy(people, 'age');

        // groupedPeople is:
        // {
        //   20: [
        //     { name: 'Max', age: 20 },
        //     { name: 'Jane', age: 20 }
        //   ],
        //   21: [{ name: 'Alice', age: 21 }]
        // }
        console.log(groupedPeople);


        // 6. 使用扩展运算符和 initialValue 绑定包含在对象数组中的数组

        let friends = [{
            name: 'Anna',
            books: ['Bible', 'Harry Potter'],
            age: 21
        }, {
            name: 'Bob',
            books: ['War and peace', 'Romeo and Juliet'],
            age: 26
        }, {
            name: 'Alice',
            books: ['The Lord of the Rings', 'The Shining'],
            age: 18
        }]

        let allbooks = friends.reduce(function (previousValue, currentValue) {
            return [...previousValue, ...currentValue.books]
        }, ['Alphabet']);

        // allbooks = [
        //   'Alphabet', 'Bible', 'Harry Potter', 'War and peace',
        //   'Romeo and Juliet', 'The Lord of the Rings',
        //   'The Shining'
        // ]

        // 7. 数组去重

        let myArray = ['a', 'b', 'a', 'b', 'c', 'e', 'e', 'c', 'd', 'd', 'd', 'd']
        let myArrayWithNoDuplicates = myArray.reduce(function (previousValue, currentValue) {
            if (previousValue.indexOf(currentValue) === -1) {
                previousValue.push(currentValue)
            }
            return previousValue
        }, []);

        console.log(myArrayWithNoDuplicates)
    </script>
</body>

</html>